package com.ww.word.ui;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.os.Bundle;
import android.os.Handler;

import com.ww.core.activity.MyActivity;
import com.ww.core.util.CoreSharedPreferencesHelper;
import com.ww.core.util.DeviceUtil;
import com.ww.core.util.StringUtils;
import com.ww.core.widget.DetailView;
import com.ww.word.R;

/**
 * @since 
 * @version 2013-12-12
 * @author wangwei
 */
public class GroupActivity extends MyActivity {
	private DetailView t;
	private String str = "";
	private String name;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.core_detail);
		name = getIntent().getStringExtra("name");
		setTitle(name);
		t = (DetailView) findViewById(R.id.content);
		CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(GroupActivity.this);
		str = h.getValue(name);
		if (!StringUtils.isNotEmpty(str)) {
			if (DeviceUtil.checkNet(this)) {
				showProgressDialog(this);
				new Thread() {
					public void run() {
						getMsg();
					}
				}.start();
			}
		} else {
			handler.sendEmptyMessage(1);
		}

	}

	public void getMsg() {
		Document document = null;
		try {
			document = Jsoup.connect("http://dict.baidu.com/s?wd=" + StringUtils.toUtf8String(name)).timeout(8000)
					.get();
			if (document != null) {
				try {
					str = "";
					String temp = document.getElementById("cn-detailmean").getElementsByTag("ul").html();
					if (StringUtils.isNotEmpty(temp)) {
						str += "<font color='#515151' >" + getString(R.string.detail_detailmean) + "</font><br>" + temp;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					String temp = document.getElementById("cn-baike-mean").getElementsByTag("div").get(0).html();
					if (StringUtils.isNotEmpty(temp)) {
						str += "<font color='#515151' >" + getString(R.string.detail_baike) + "</font><br>" + temp;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					String temp = document.getElementById("cn-syn-ant").getElementsByTag("ul").html();
					if (StringUtils.isNotEmpty(temp)) {
						str += "<font color='#515151' >" + getString(R.string.detail_syn) + "</font><br>" + temp;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

				CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(GroupActivity.this);
				h.setValue(name, str);
				handler.sendEmptyMessage(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			hideProgressDialog();
			t.setText(str);
		};
	};

}
